import logging

import allure
import jsonpath

from utils.request.request_control import RequestControl


@allure.step('http断言')
def json_assert(case, resp):
    expected = case['expected']
    # print('resp::::::::::', resp.text, case['json_check'])
    if case['json_check']:
        result = jsonpath.jsonpath(resp.json(), case['json_check'])[0]
        logging.info(f"\n====预期结果({expected}) == 实际结果({result})")
        assert result == expected
    else:
        logging.info(f"\n====预期结果({expected}) in 实际结果({resp.text})")
        assert expected in resp.text


def jdbc_assert(case):
    if case['sql_check']:
        with allure.step('jdbc断言'):
            expected = case['sql_expected']
            result = RequestControl(case).jdbc_request(case['sql_check'])
            logging.info(f"\n====预期结果({expected}) in 实际结果({result})")
            assert result == expected